package org.yf.samples.dao;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.yf.samples.model.UserSubject;

@Repository
@Transactional
public class UserSubjectDAOImpl extends BaseHibernateDAO<UserSubject> implements IUserSubjectDAO {

	@Override
	public List<UserSubject> FindAllUserSubjects() {
		return this.find("FROM UserSubject");
	}

	@Override
	public void deleteUserSubjectBySubId(Integer subId) {
		List<UserSubject> userSubject = getUserSubjectsBySubId(subId);
		if(userSubject != null){
			this.deleteAll(userSubject);
		}
	}

	@Override
	public List<UserSubject> getUserSubjectsBySubId(Integer subId) {
		return this.queryForList("FROM UserSubject AS us WHERE us.subject.subId=? ", new Object[]{subId});
	}

	@Override
	public List<UserSubject> getUserSubjectsByUserId(String userId) {
		return this.queryForList("FROM UserSubject AS us WHERE us.user.userId=? ", new Object[]{userId});
	}

	@Override
	public void insertSubjectUsers(List<String> insertList, Integer subId) {
		String sql = null;
		for(String userId : insertList){
			sql = "insert into exam_user_subject(user_id, sub_id) values(:userId, :subId)";
			this.createSqlQuery(sql).setString("userId", userId).setInteger("subId", subId).executeUpdate();			
		}
/*		List<UserSubject> userSubjectList = new ArrayList<UserSubject>();
		Subject subject = new Subject();
		subject.setSubId(subId);
		for(String userId : insertList){
			UserSubject userSubject = new UserSubject();
			User user = new User();
			user.setUserId(userId);
			userSubject.setSubject(subject);
			userSubject.setUser(user);
			userSubjectList.add(userSubject);
		}
		this.save(userSubjectList);*/
	}

	@Override
	public void deleteSubjectUsers(List<String> delList, Integer subId) {
		String sql = null;
		for(String userId : delList){
			sql = "delete exam_user_subject from exam_user_subject where user_id = :userId and sub_id= :subId";
			this.createSqlQuery(sql).setString("userId", userId).setInteger("subId", subId).executeUpdate();
		}
/*		List<UserSubject> userSubjectList = new ArrayList<UserSubject>();
		Subject subject = new Subject();
		subject.setSubId(subId);
		for(String userId : delList){
			UserSubject userSubject = new UserSubject();
			User user = new User();
			user.setUserId(userId);
			userSubject.setSubject(subject);
			userSubject.setUser(user);
			userSubjectList.add(userSubject);
		}
		this.deleteAll(userSubjectList);*/
	}

	@Override
	public void insertStudentSubjects(List<Integer> insertList, String userId) {
		String subjectSql = null;
		for(Integer subId : insertList){
			subjectSql = "insert into exam_user_subject(user_id, sub_id) values(:userId, :subId)";
			this.createSqlQuery(subjectSql).setString("userId", userId).setInteger("subId", subId).executeUpdate();
		}
	}

	@Override
	public void deleteStudentSubjects(List<Integer> delList, String userId) {
		String subjectSql = null;
		for(Integer subId : delList){
			subjectSql = "delete exam_user_subject from exam_user_subject where user_id = :userId and sub_id= :subId";
			this.createSqlQuery(subjectSql).setString("userId", userId).setInteger("subId", subId).executeUpdate();
		}
	}

}
